home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / communication / bbs / termv4.6 / extras / source / term-source.lha / Raster.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-18  |  8.9 KB  |  515 lines

  1. /*
  2. **    Raster.c
  3. **
  4. **    Screen character (raster) buffering routines
  5. **
  6. **    Copyright © 1990-1996 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10. #ifndef _GLOBAL_H
  11. #include "Global.h"
  12. #endif
  13.  
  14.     /* DeleteRaster():
  15.      *
  16.      *    Free the contents of the character raster.
  17.      */
  18.  
  19. VOID
  20. DeleteRaster()
  21. {
  22.     FreeVecPooled(RasterSemaphore);
  23.     RasterSemaphore = NULL;
  24.  
  25.     FreeVecPooled(Raster);
  26.     Raster = NULL;
  27.  
  28.     FreeVecPooled(RasterAttr);
  29.     RasterAttr = NULL;
  30. }
  31.  
  32.     /* CreateRaster():
  33.      *
  34.      *    Create the character raster.
  35.      */
  36.  
  37. BOOL
  38. CreateRaster()
  39. {
  40.         /* Width of the screen * 2 (in characters),
  41.          * extra for double width. The window size
  42.          * may change, the screen size hopefully
  43.          * doesn't.
  44.          */
  45.  
  46.     RasterWidth    = (Window -> WScreen -> Width / TextFontWidth) * 2;
  47.  
  48.         /* Height of the character raster. */
  49.  
  50.     RasterHeight    = Window -> WScreen -> Height / TextFontHeight;
  51.  
  52.         /* Allocate the raster. */
  53.  
  54.     if(Raster = (STRPTR)AllocVecPooled(RasterWidth * RasterHeight,MEMF_ANY | MEMF_CLEAR))
  55.     {
  56.             /* Allocate the raster attributes. */
  57.  
  58.         if(RasterAttr = (STRPTR)AllocVecPooled(RasterHeight,MEMF_ANY | MEMF_CLEAR))
  59.         {
  60.             if(RasterSemaphore = (struct SignalSemaphore *)AllocVecPooled(sizeof(struct SignalSemaphore),MEMF_ANY))
  61.             {
  62.                 InitSemaphore(RasterSemaphore);
  63.  
  64.                 return(TRUE);
  65.             }
  66.         }
  67.     }
  68.  
  69.     DeleteRaster();
  70.  
  71.     return(FALSE);
  72. }
  73.  
  74.     /* RasterEraseScreen(BYTE Mode):
  75.      *
  76.      *    Erase parts of the screen.
  77.      */
  78.  
  79. VOID
  80. RasterEraseScreen(LONG Mode)
  81. {
  82.     LONG    First,
  83.         Last;
  84.  
  85.     ObtainSemaphore(RasterSemaphore);
  86.  
  87.     switch(Mode)
  88.     {
  89.         case 1:    First    = 0;
  90.             Last    = CursorY * RasterWidth + CursorX + 1;
  91.  
  92.             if(CursorY == LastLine)
  93.                 SaveRaster(0,CursorY);
  94.  
  95.             memset(RasterAttr,SCALE_NORMAL,CursorY + 1);
  96.  
  97.             break;
  98.  
  99.         case 2:    First    = 0;
  100.             Last    = RasterHeight * RasterWidth - 1;
  101.  
  102.             SaveRaster(0,RasterHeight - 1);
  103.  
  104.             memset(RasterAttr,SCALE_NORMAL,RasterHeight);
  105.  
  106.             break;
  107.  
  108.         default:First    = CursorY * RasterWidth + CursorX;
  109.             Last    = RasterHeight * RasterWidth - 1;
  110.  
  111.             if(CursorY == 0)
  112.                 SaveRaster(CursorY,RasterHeight - 1);
  113.  
  114.             memset(&RasterAttr[CursorY],SCALE_NORMAL,RasterHeight - CursorY);
  115.  
  116.             break;
  117.     }
  118.  
  119.     RethinkRasterLimit();
  120.  
  121.     if(Last > First)
  122.         memset(&Raster[First],' ',Last - First);
  123.  
  124.     ConFontScaleUpdate();
  125.  
  126.     ReleaseSemaphore(RasterSemaphore);
  127. }
  128.  
  129.     /* RasterEraseLine(BYTE Mode):
  130.      *
  131.      *    Erase parts of the current cursor line.
  132.      */
  133.  
  134. VOID
  135. RasterEraseLine(LONG Mode)
  136. {
  137.     LONG    First,
  138.         Last;
  139.  
  140.     ObtainSemaphore(RasterSemaphore);
  141.  
  142. //    SaveRaster(CursorY,CursorY);
  143.  
  144.     switch(Mode)
  145.     {
  146.             /* From beginning to current cursor position. */
  147.  
  148.         case 1:    First    = CursorY * RasterWidth;
  149.             Last    = First + CursorX + 1;
  150.  
  151.             break;
  152.  
  153.             /* Entire line. */
  154.  
  155.         case 2:    First    = CursorY * RasterWidth;
  156.             Last    = First + RasterWidth - 1;
  157.  
  158.             break;
  159.  
  160.             /* From current cursor position towards end. */
  161.  
  162.         default:First    = CursorY * RasterWidth + CursorX;
  163.             Last    = (CursorY + 1) * RasterWidth - 1;
  164.  
  165.             break;
  166.     }
  167.  
  168.     if(Last > First)
  169.         memset(&Raster[First],' ',Last - First);
  170.  
  171.     ReleaseSemaphore(RasterSemaphore);
  172. }
  173.  
  174.     /* RasterEraseCharacters(LONG Chars):
  175.      *
  176.      *    Erase a number of characters in the current cursor
  177.      *    line.
  178.      */
  179.  
  180. VOID
  181. RasterEraseCharacters(LONG Chars)
  182. {
  183.     if(CursorX < RasterWidth - 1)
  184.     {
  185.         LONG     First,
  186.              Diff;
  187.         UBYTE    *To,
  188.             *From;
  189.  
  190. //        SaveRaster(CursorY,CursorY);
  191.  
  192.         ObtainSemaphore(RasterSemaphore);
  193.  
  194.         First    = CursorY * RasterWidth + CursorX;
  195.         To    = &Raster[First];
  196.  
  197.         if(CursorX + Chars >= RasterWidth)
  198.         {
  199.             Diff = RasterWidth - 1 - CursorX;
  200.  
  201.             while(Diff-- > 0)
  202.                 *To++ = ' ';
  203.         }
  204.         else
  205.         {
  206.             From    = &Raster[First + Chars];
  207.             Diff    = RasterWidth - (CursorX + 1 + Chars);
  208.  
  209.             while(Diff--)
  210.             {
  211.                 *To++ = *From;
  212.  
  213.                 *From++ = ' ';
  214.             }
  215.         }
  216.  
  217.         ReleaseSemaphore(RasterSemaphore);
  218.     }
  219. }
  220.  
  221.     /* RasterClearLine(LONG Lines):
  222.      *
  223.      *    Clear and remove a number of lines.
  224.      */
  225.  
  226. VOID
  227. RasterClearLine(LONG Lines,LONG Top)
  228. {
  229.     if(Lines)
  230.     {
  231.         LONG RegionBottom;
  232.  
  233.         ObtainSemaphore(RasterSemaphore);
  234.  
  235.         if(RegionSet)
  236.             RegionBottom = Bottom;
  237.         else
  238.             RegionBottom = LastLine + 1;
  239.  
  240.         if(Top + Lines >= RegionBottom + 1)
  241.         {
  242.             SaveRaster(Top,RegionBottom);
  243.  
  244.             Lines = RegionBottom - Top + 1;
  245.  
  246.             memset(&Raster[Top * RasterWidth],' ',RasterWidth * Lines);
  247.             memset(&RasterAttr[Top * RasterWidth],SCALE_NORMAL,RasterWidth * Lines);
  248.         }
  249.         else
  250.         {
  251.             LONG     Max;
  252.             UBYTE    *From,
  253.                 *To;
  254.  
  255.             SaveRaster(Top,Top + Lines - 1);
  256.  
  257.             Max    = (RegionBottom - (Top + Lines)) * RasterWidth;
  258.  
  259.             From    = &Raster[(Top + Lines) * RasterWidth];
  260.             To    = &Raster[ Top          * RasterWidth];
  261.  
  262.             while(Max--)
  263.             {
  264.                 *To++ = *From;
  265.  
  266.                 *From++ = ' ';
  267.             }
  268.  
  269.             memset(&RasterAttr[RegionBottom - Lines],SCALE_NORMAL,Lines);
  270.         }
  271.  
  272.         RethinkRasterLimit();
  273.  
  274.         ConFontScaleUpdate();
  275.  
  276.         ReleaseSemaphore(RasterSemaphore);
  277.     }
  278. }
  279.  
  280.     /* RasterInsertLine(LONG Lines):
  281.      *
  282.      *    Insert a number of lines at the current cursor line.
  283.      */
  284.  
  285. VOID
  286. RasterInsertLine(LONG Lines,LONG Top)
  287. {
  288.     if(Lines)
  289.     {
  290.         LONG RegionBottom;
  291.  
  292.         if(RegionSet)
  293.             RegionBottom = Bottom;
  294.         else
  295.             RegionBottom = LastLine + 1;
  296.  
  297.         if(Top + Lines >= RegionBottom + 1)
  298.         {
  299.             SaveRaster(Top,RegionBottom);
  300.  
  301.             Lines = RegionBottom - Top + 1;
  302.  
  303.             memset(&Raster[Top * RasterWidth],' ',RasterWidth * Lines);
  304.             memset(&RasterAttr[Top * RasterWidth],SCALE_NORMAL,RasterWidth * Lines);
  305.         }
  306.         else
  307.         {
  308.             LONG     From,To,
  309.                  Max;
  310.             UBYTE    *FromPtr,
  311.                 *ToPtr;
  312.  
  313.             SaveRaster(RegionBottom - Lines,RegionBottom);
  314.  
  315.             ObtainSemaphore(RasterSemaphore);
  316.  
  317.             Max    = (RegionBottom - Lines - Top) * RasterWidth;
  318.  
  319.             From    = (RegionBottom - Lines) * RasterWidth - 1;
  320.             To    =  RegionBottom          * RasterWidth - 1;
  321.  
  322.             FromPtr    = &Raster[From];
  323.             ToPtr    = &Raster[To];
  324.  
  325.             while(Max--)
  326.                 *ToPtr-- = *FromPtr--;
  327.  
  328.             memset(&Raster[Top * RasterWidth],' ',Lines * RasterWidth);
  329.  
  330.             ReleaseSemaphore(RasterSemaphore);
  331.         }
  332.     }
  333. }
  334.  
  335.     /* RasterScrollRegion(LONG Direction,LONG RasterTop,LONG RasterBottom,LONG RasterLines):
  336.      *
  337.      *    Scroll the contents of the character raster up/down.
  338.      */
  339.  
  340. VOID
  341. RasterScrollRegion(LONG Direction,LONG RasterTop,LONG RasterBottom,LONG RasterLines)
  342. {
  343.     LONG Dir = ABS(Direction);
  344.  
  345.     ObtainSemaphore(RasterSemaphore);
  346.  
  347.     SaveRaster(RasterTop,RasterTop + RasterLines - 1);
  348.  
  349.     if(Dir >= RasterLines)
  350.     {
  351.             /* All that is needed is to delete the lines. */
  352.  
  353.         memset(&Raster[RasterTop * RasterWidth],' ',RasterLines * RasterWidth);
  354.     }
  355.     else
  356.     {
  357.         LONG     First,
  358.              Last,
  359.              Max,
  360.              i;
  361.         UBYTE    *From,
  362.             *To;
  363.  
  364.         Max = (RasterLines - Dir) * RasterWidth;
  365.  
  366.         if(Direction < 0)
  367.         {
  368.             First    = (RasterTop + RasterLines - Dir) * RasterWidth - 1;
  369.             Last    = (RasterTop + RasterLines    ) * RasterWidth - 1;
  370.  
  371.             From    = &Raster[First];
  372.             To    = &Raster[Last];
  373.  
  374.             while(Max--)
  375.                 *To-- = *From--;
  376.  
  377.             for(i = RasterBottom ; i >= (RasterTop + Dir) ; i--)
  378.                 RasterAttr[i] = RasterAttr[i - Dir];
  379.  
  380.             memset(&Raster[RasterTop * RasterWidth],' ',RasterWidth * Dir);
  381.  
  382.             memset(&RasterAttr[RasterTop],SCALE_NORMAL,Dir);
  383.         }
  384.         else
  385.         {
  386.             First    = RasterTop * RasterWidth + RasterWidth * Dir;
  387.             Last    = RasterTop * RasterWidth;
  388.  
  389.             From    = &Raster[First];
  390.             To    = &Raster[Last];
  391.  
  392.             while(Max--)
  393.                 *To++ = *From++;
  394.  
  395.             memset(&Raster[(RasterBottom - Dir) * RasterWidth],' ',RasterWidth * Dir);
  396.  
  397.             for(i = RasterTop ; i <= (RasterBottom - Dir) ; i++)
  398.                 RasterAttr[i] = RasterAttr[i + Dir];
  399.  
  400.             memset(&RasterAttr[RasterBottom - Dir],SCALE_NORMAL,Dir);
  401.         }
  402.     }
  403.  
  404.     RethinkRasterLimit();
  405.  
  406.     ConFontScaleUpdate();
  407.  
  408.     ReleaseSemaphore(RasterSemaphore);
  409. }
  410.  
  411.     /* RasterShiftChar(LONG Size):
  412.      *
  413.      *    Shift the characters following the current cursor
  414.      *    position Size characters to the right.
  415.      */
  416.  
  417. VOID
  418. RasterShiftChar(LONG Size)
  419. {
  420.     LONG     i,
  421.          First;
  422.     UBYTE    *From,
  423.         *To;
  424.  
  425.     ObtainSemaphore(RasterSemaphore);
  426.  
  427.     if(CursorX + Size >= RasterWidth - 1)
  428.     {
  429.         i    = RasterWidth - 1 - CursorX;
  430.         To    = &Raster[CursorY * RasterWidth + CursorX];
  431.  
  432.         while(i-- > 0)
  433.             *To++ = ' ';
  434.     }
  435.     else
  436.     {
  437.         First    = (CursorY + 1) * RasterWidth - 1;
  438.         To    = &Raster[First];
  439.  
  440.         From    = &Raster[First - Size];
  441.         i    = RasterWidth - Size;
  442.  
  443.         while(i-- > CursorX)
  444.             *To-- = *From--;
  445.  
  446.         To    = &Raster[CursorY * RasterWidth + CursorX];
  447.  
  448.         while(Size--)
  449.             *To++ = ' ';
  450.     }
  451.  
  452.     ReleaseSemaphore(RasterSemaphore);
  453. }
  454.  
  455.     /* RasterPutString(STRPTR String,LONG Length):
  456.      *
  457.      *    Put a string into the character raster.
  458.      */
  459.  
  460. VOID
  461. RasterPutString(STRPTR String,LONG Length)
  462. {
  463.     ObtainSemaphore(RasterSemaphore);
  464.  
  465.     if(Length == 1)
  466.     {
  467.         if(CursorX + 1 < RasterWidth)
  468.             Raster[CursorY * RasterWidth + CursorX] = String[0];
  469.     }
  470.     else
  471.     {
  472.         if(CursorX + Length >= RasterWidth)
  473.             Length = RasterWidth - 1 - CursorX;
  474.  
  475.         if(Length > 0)
  476.             CopyMem(String,&Raster[CursorY * RasterWidth + CursorX],Length);
  477.     }
  478.  
  479.     ReleaseSemaphore(RasterSemaphore);
  480. }
  481.  
  482. VOID
  483. SaveRasterDummy(LONG First,LONG Last)
  484. {
  485. }
  486.  
  487. VOID
  488. SaveRasterReal(LONG First,LONG Last)
  489. {
  490.     STRPTR    Line,This;
  491.     LONG    Size;
  492.     LONG    i;
  493.  
  494.     ObtainSemaphore(RasterSemaphore);
  495.  
  496.     if(First < 0)
  497.         First = 0;
  498.  
  499.     if(Last > RasterHeight - 1)
  500.         Last = RasterHeight - 1;
  501.  
  502.     for(i = First, This = &Raster[(LONG)First * RasterWidth] ; i <= Last ; i++, This += RasterWidth)
  503.     {
  504.         Line = This;
  505.         Size = RasterWidth - 1;
  506.  
  507.         while(Size > 0 && Line[Size - 1] == ' ')
  508.             Size--;
  509.  
  510.         AddLine(Line,Size);
  511.     }
  512.  
  513.     ReleaseSemaphore(RasterSemaphore);
  514. }
  515.